package cn.micai.base.task;

import java.math.BigInteger;
import java.util.concurrent.BlockingQueue;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/7/16 11:21
 * @Description: 不可靠的取消把生产者置于阻塞的操作中(不要这样做)
 */
public class BrokenPrimeProducer extends Thread {

    private final BlockingQueue<BigInteger> queue;

    private volatile boolean cancelled = false;

    BrokenPrimeProducer(BlockingQueue<BigInteger> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        try {
            BigInteger p = BigInteger.ONE;
            while (!cancelled) {
                queue.put(p = p.nextProbablePrime());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void cancel() {
        cancelled = true;
    }

}
